package com.study.netty2;

import io.netty.channel.Channel;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author mjy
 * @date 2021/12/07
 * @description
 */
public class ChannelMap {

    private static Map<String, Channel> map = new ConcurrentHashMap<>();

    /**
     * 添加管道
     * (唯一标识符，比如imei)
     *
     * @param clientId
     * @param channel
     */
    public static void add(String clientId, Channel channel) {
        map.put(clientId, channel);
    }

    /**
     * 根据唯一标识
     *
     * @param clientId
     * @return
     */
    public static Channel get(String clientId) {
        return map.get(clientId);
    }

    /**
     * 根据key移除管道
     *
     * @param key
     */
    public static void removerByKey(String key) {
        map.remove(key);
    }

    /**
     * 根据管道获取key
     */

    public static String getKey(Channel channel) {
        String key = "";
        for (Map.Entry<String,Channel> entry:map.entrySet()){
            if (entry.getValue()==channel){
                key = entry.getKey();
            }
        }
        return key;
    }




}
